﻿package com.urbansquall.ginger.tools
{
	
import com.urbansquall.ginger.AnimationPlayer;

import flash.utils.Dictionary;


/**
 * 
 * @author Panayoti
 * @description Utility class for creating a few AnimationPlayers that can be created easily
 * 
 */
public class AnimationPlayerFactory
{
	private var m_animations : Dictionary;
	
	public function AnimationPlayerFactory()
	{
		m_animations = new Dictionary();
	}
	
	/**
	 * Adds a new AnimationPlayer to the factory 
	 * @param a_name a String used to reference the AnimationPlayer
	 * @param a_animationPlayer The instance of the AnimationPlayer to add to the factory
	 * 
	 */	
	public function register( a_name : String, a_animationPlayer : AnimationPlayer ) : void
	{
		if( m_animations[ a_name ] != null )
		{
			throw new Error( "AnimationFactory:register. There is already an animation with the name [" + a_name + "]" );
		}
		
		m_animations[ a_name ] = a_animationPlayer;
	}
	
	/**
	 * Create a new AnimationPlayer 
	 * @param a_name The reference to the AnimationPlayer you would like to create
	 * @return A new instance of the AnimationPlayer
	 * 
	 */	
	public function instantiate( a_name : String ) : AnimationPlayer
	{
		if( m_animations[ a_name ] == null )
		{
			throw new Error( "AnimationFactory:instantiate. There is no animation with the name [" + a_name + "]" );
		}
		
		var animationPlayer : AnimationPlayer = m_animations[ a_name ];
		return animationPlayer.copy();
	}
	
	/**
	 * Cleans up and removes all references to the AnimationPlayer and trashes this factory. 
	 * 
	 */	
	public function dispose() : void
	{
		for( var key : String in m_animations )
		{
			var animationPlayer : AnimationPlayer = m_animations[ key ];
			// animationPlayer.dispose();
			delete m_animations[ key ];
		}
		
		m_animations = null;
	}
	
}
}